package com.qipay.ccbCallback.impl;


import com.ccb.wlpt.RequestProcess;
import com.qipay.ccbCallback.CcbCallbackService;
import com.qipay.order.service.OrderService;
import com.qipay.sp.dto.CcbBankOrderListDto;
import com.qipay.utils.DateUtils;
import com.qipay.utils.XmlUtils;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CcbCallbackServiceImpl implements CcbCallbackService {

    @Override
    public List<CcbBankOrderListDto> queryFlows(String posNum) {
        //定时方法，每分钟一次
        String initResult = RequestProcess.initFileCert("105001453995620",
                "105001453995620-123","/usr/local/hhh.pfx","qwe123",
                "/usr/local/config.xml");
//        String initResult = RequestProcess.initFileCert("105001453995620",
//                "105001453995620-123","C:\\Users\\Administrator\\Desktop\\hhh.pfx","qwe123",
//                "C:\\Users\\Administrator\\Desktop\\config.xml");
        List<CcbBankOrderListDto> resultXml=new ArrayList<>();
        SimpleDateFormat sdf14 = new SimpleDateFormat("yyyyMMdd");
        Date minDate=new Date(new Date().getTime() - 15*60*1000);
        SimpleDateFormat h = new SimpleDateFormat("HH");
        SimpleDateFormat m = new SimpleDateFormat("mm");
        if("".equals(initResult)){
            String xmlStr = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>  \n" +
                    "<TX>  \n" +
                    "  <REQUEST_SN>123456</REQUEST_SN>  \n" +
                    "  <CUST_ID>105001453995620</CUST_ID>  \n" +
                    "  <USER_ID>105001453995620-123</USER_ID>  \n" +
                    "  <PASSWORD>123456</PASSWORD>  \n" +
                    "  <TX_CODE>5W1002</TX_CODE>  \n" +
                    "  <LANGUAGE>CN</LANGUAGE>  \n" +
                    "  <TX_INFO>  \n" +
                    "    <START>"+ sdf14.format(new Date()) +"</START>  \n" +
                    "    <STARTHOUR>"+h.format(minDate)+"</STARTHOUR>  \n" +
                    "    <STARTMIN>"+m.format(minDate)+"</STARTMIN>  \n" +
                    "    <END>"+sdf14.format(new Date())+"</END>  \n" +
                    "    <ENDHOUR>23</ENDHOUR>  \n" +
                    "    <ENDMIN>59</ENDMIN>  \n" +
                    "    <KIND>0</KIND>  \n" +
                    "    <DEXCEL>1</DEXCEL>  \n" +
                    "    <NORDERBY>1</NORDERBY>  \n" +
                    "    <PAGE>1</PAGE>  \n" +
                    "    <POS_CODE>"+posNum+"</POS_CODE>  \n" +
                    "    <STATUS>1</STATUS>  \n" +
                    "  </TX_INFO>  \n" +
                    "</TX> \n";
            String result = RequestProcess.sendRequest("https://merchant.ccb.com",
                    xmlStr,false);
            try {
                resultXml=XmlUtils.toMapBean(result);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
        return resultXml;
    }

    public static void main(String[] args){
        String initResult = RequestProcess.initFileCert("105001453995620",
                "105001453995620-123","C:\\Users\\Administrator\\Desktop\\hhh.pfx","qwe123",
                "C:\\Users\\Administrator\\Desktop\\config.xml");

        if("".equals(initResult)){
            String xmlStr = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>  \n" +
                    "<TX>  \n" +
                    "  <REQUEST_SN>123456</REQUEST_SN>  \n" +
                    "  <CUST_ID>105001453995620</CUST_ID>  \n" +
                    "  <USER_ID>105001453995620-123</USER_ID>  \n" +
                    "  <PASSWORD>123456</PASSWORD>  \n" +
                    "  <TX_CODE>5W1002</TX_CODE>  \n" +
                    "  <LANGUAGE>CN</LANGUAGE>  \n" +
                    "  <TX_INFO>  \n" +
                    "    <START>20190110</START>  \n" +
                    "    <STARTHOUR>00</STARTHOUR>  \n" +
                    "    <STARTMIN>00</STARTMIN>  \n" +
                    "    <END>20190110</END>  \n" +
                    "    <ENDHOUR>23</ENDHOUR>  \n" +
                    "    <ENDMIN>59</ENDMIN>  \n" +
                    "    <KIND>0</KIND>  \n" +
                    "    <DEXCEL>1</DEXCEL>  \n" +
                    "    <NORDERBY>1</NORDERBY>  \n" +
                    "    <PAGE>1</PAGE>  \n" +
                    "    <POS_CODE>031571622</POS_CODE>  \n" +
                    "    <STATUS>1</STATUS>  \n" +
                    "  </TX_INFO>  \n" +
                    "</TX> \n";
            String result = RequestProcess.sendRequest("https://merchant.ccb.com",
                    xmlStr,false);
            List<CcbBankOrderListDto> resultxml;
            try {
               resultxml=XmlUtils.toMapBean(result);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            System.out.println(result);
        }

//        String xmlStr = "<?xml version=\"1.0\" encoding=\"GB18030\" standalone=\"yes\" ?>  \n" +
//                "<TX>   \n" +
//                "  <REQUEST_SN>123456</REQUEST_SN>  \n" +
//                "  <CUST_ID>105001453995620</CUST_ID>  \n" +
//                "  <USER_ID>105001453995620-123</USER_ID>  \n" +
//                "  <PASSWORD>qwe123</PASSWORD>  \n" +
//                "  <TX_CODE>5W1005</TX_CODE>  \n" +
//                "  <LANGUAGE>CN</LANGUAGE>  \n" +
//                "  <TX_INFO>  \n" +
//                "    <DATE>20190109</DATE>  \n" +
//                "    <KIND>0</KIND>  \n" +
//                "    <FILETYPE>2</FILETYPE>  \n" +
//                "    <TYPE>0</TYPE>  \n" +
//                "    <NORDERBY>1</NORDERBY>  \n" +
//                "    <POS_CODE>03157620</POS_CODE>  \n" +
//                "    <STATUS>1</STATUS>  \n" +
//                "    <BILL_FLAG>1</BILL_FLAG>  \n" +
//                "  </TX_INFO>  \n" +
//                "</TX> \n";
//        String result = RequestProcess.downloadFile("https://merchant.ccb.com",
//                xmlStr,"D:\\payment");
//        System.out.println(result);
    }

}
